﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormTest.Entity
{
    public class User
    {
        [Category("基本信息")]
        public string Name { get; set; }

        [Category("基本信息")]
        public int Age { get; set; }

        [Category("账户信息")]
        public string Email { get; set; }
        public User Clone() => new User { Name = this.Name, Age = this.Age };
    }

    public class UserCollection
    {
        private List<User> _users = new List<User>();

        [DisplayName("用户列表")]
        [Editor(typeof(CollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public List<User> Users
        {
            get { return _users; }
            set { _users = value; }
        }
    }
    public class UserCollectionEditor : CollectionEditor
    {
        public UserCollectionEditor(Type type) : base(type) { }

        protected override CollectionForm CreateCollectionForm()
        {
            var form = base.CreateCollectionForm();
            var addButton = form.Controls.Find("addButton", true)[0] as Button;

            // 添加复制按钮
            var copyButton = new Button
            {
                Text = "复制",
                Top = addButton.Top,
                Left = addButton.Left + addButton.Width + 5,
                Width = addButton.Width
            };

            copyButton.Click += (s, e) => {
                if (Context.Instance is List<User> list && list.Count > 0)
                {
                    var selected = list[list.Count - 1]; // 获取最后一项
                    list.Add(selected.Clone());
                    RefreshList();
                }
            };

            addButton.Parent.Controls.Add(copyButton);
            return form;
        }

        private void RefreshList()
        {
            
        }
    }
}
